﻿
Imports Oracle.DataAccess.Client
Public Class fListaNotasCredito
    Private lDocumento As String
    Private lNumero As Integer
    Public ReadOnly Property pDocumento() As String
        Get
            Return lDocumento
        End Get
    End Property

    Public ReadOnly Property pNumero() As Integer
        Get
            Return lNumero
        End Get
    End Property

    Private Sub btnBuscaCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCliente.Click
        Try
            Dim flcli As New fBuscaTercero
            flcli.TipoTercero = "Cliente"
            If flcli.ShowDialog = Windows.Forms.DialogResult.OK Then

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub fListaNotasCredito_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dtpFechaDesde.Value = dtpFechaHasta.Value.AddDays(-30)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        cmd.CommandText = "SELECT * FROM MOV_MOVIMIENTO MOV,GLO_TIPODOCUMENTO DOC "
        Dim qWhere As String = "WHERE MOV.IDDOCUMENTO = DOC.TIPODOC AND DOC.DOCES=13 "
        Dim CuentaW As Int16 = 1
        If Me.dtpFechaDesde.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= " AND MOV.FECHADOCUMENTO >= '" & FormatDateTime(Me.dtpFechaDesde.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.dtpFechaHasta.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
               qWhere &= " AND "
            CuentaW += 1
            'qWhere &= "FECHA <= TO_DATE('26012009','DDMMYYYY')"
            qWhere &= "MOV.FECHADOCUMENTO <= '" & FormatDateTime(Me.dtpFechaHasta.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.txtTercero.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "MOV.IDTERCERO =" & Me.txtTercero.Text & ""
        End If
        If Me.txtSucursal.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "MOV.SUCURSAL ='" & Me.txtSucursal.Text & "'"
        End If
        If CuentaW = 0 Then _
            qWhere = ""
        cmd.CommandText &= qWhere
        Try
            Conn.Open()
            DsMovimiento.MOV_MOVIMIENTO.Rows.Clear()
            DsMovimiento.MOV_MOVIMIENTO.Load(cmd.ExecuteReader)
            MOV_MOVIMIENTODataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub


    Private Sub AsignaNotaCredito(ByVal Fila As Integer)
        If Fila >= 0 Then
            lDocumento = MOV_MOVIMIENTODataGridView.Rows(Fila).Cells("IDDOCUMENTO").Value
            lNumero = MOV_MOVIMIENTODataGridView.Rows(Fila).Cells("NUMERO").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub MOV_MOVIMIENTODataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MOV_MOVIMIENTODataGridView.CellMouseDoubleClick
        AsignaNotaCredito(e.RowIndex)
    End Sub


    Private Sub MOV_MOVIMIENTODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_MOVIMIENTODataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaNotaCredito(MOV_MOVIMIENTODataGridView.CurrentRow.Index)
        End If
    End Sub
End Class